package com.itheima.health.controller;

import com.aliyuncs.exceptions.ClientException;
import com.itheima.health.entity.Result;
import com.itheima.health.service.OrderService;
import com.itheima.health.utils.redis.RedisMessageConstant;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.sms.SmsUtils;
import com.itheima.health.utils.sms.ValidateCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("order")
@Api(tags = "移动端预约管理")
public class OrderController {
    @Reference
    private OrderService orderService;

    /**
     *order/sendCode
     * @param telephone  用户的手机号码
     * @return  生成的验证码
     */
    @GetMapping("sendCode/{telephone}")
    public Result sendCode(@PathVariable("telephone")String telephone)  {
        String code = ValidateCodeUtils.generateValidateCode(4);
         SmsUtils.validUserTelephone(telephone,code);
//        Map map = new HashMap();
//        map.put("telephone",telephone);
//        map.put("code",code);
//        rabbitTemplate.convertAndSend("heima-queue",map);
        System.out.println("----验证码："+code);
        RedisUtil.set(RedisMessageConstant.SENDTYPE_ORDER+telephone,code,60, TimeUnit.SECONDS);
        return  new Result(code);



    }
    /**
     * 根据套餐id用户预约
     * @param map 前端传过来的数据
     * @return 订单信息
     */
    @PostMapping("add")
    @ApiOperation(value = "添加预约" ,notes = "移动端预约信息")
    public Result add(@RequestBody Map map){
        //可以先传一个flag值判断是否预约成功
        return  new Result(orderService.add(map));
    }



    @GetMapping("findOrderDetailByOrderId/{id}")
    public Result findOrderDetailByOrderId(@PathVariable("id") int id){
        return new Result(orderService.findOrderDetailByOrderId(id));
    }
}
